var registerEnCtrl  = new XFApp.client();
$.views.converters({
	
});

XFApp.client.prototype.registerEn = {
		successCode : 10000,
		urlItems : {	
			step1SaveURL: basePath+"/register/saveStep1",	
			step2SaveURL: basePath+"/register/saveStep2",	
			submitURL: basePath+"/register/submit",		
			isExistURL: basePath+"/register/isExist",
		},
		
		form1Validate : function(){
			$("#regFrom1").validate({				  
				errorPlacement : function(error, element) {
					if (element.is(":radio"))
						error.appendTo(element.parent().parent());
					else if (element.is(":checkbox"))
						error.appendTo(element.parent().parent());					
					else
						error.insertAfter(element); 
						//error.appendTo(element.parent().next());
				},
		        rules: {		        	

		        	registerType:"required",		        	
		        	companyName: {
		        				  required:true,
		        				  maxlength:60
		        				 },
		        	companyAddr: {
			        			  required:true,
			        			  maxlength:80
			        			 },		        	
			        license:    "required",
			        fileImg: "required"		        
				  },
				 messages: {
					 registerType:"请选择注册企业类型",  
					 companyName:{
						         required:"请填写单位名称",
						         maxlength: $.validator.format("填写在工商局注册的名称，最多 {0} 字符"),
					 			 },
					 companyAddr:{
				         required:"请填写单位地址",
				         maxlength: $.validator.format("地址最多 {0} 字符"),
			 			 },
			 	     license:"请输入营业执照号码",
			 	     fileImg:"请上传执照图片"
			 			 
				  }
		    });
			$("#regFrom1").validate();
		},
		
		form2Validate : function(){
			$("#regFrom2").validate({				  
				errorPlacement: function(error, element) {    //错误提示在什么地方 
					if(element.is(':radio') || element.is(':checkbox')){
						error.appendTo(element.parent());
					}else{
						error.insertAfter(element);  
					}
					
				},  
		        rules: {		        	

		        	busiIndt:"required",		        	
		        	busiDirt: "required",
			        salePrct: "required",
				  },
				 messages: {
					 busiIndt:"请填写主营行业",  
					 busiDirt:"请选择主营方向",					
					 salePrct:"请填写销售产品",
			 			 
				  }
		    });
			$("#regFrom2").validate();
		},
		
		form3Validate : function(){
			$("#regFrom3").validate({
				errorPlacement: function(error, element) {    //错误提示在什么地方 
					if(element.is(':radio') || element.is(':checkbox')){
						error.appendTo(element.parent());
					}else{
						error.insertAfter(element);  
					}
					
				}, 
		        rules: {
		        	//userId:"required",		        	          
		        	realName: "required",
		        	password:  {required:true,
		        			    minlength:6,
		        		        maxlength:20
		        			   },		        	
		        	password2: {
		        		        required:true,
		        		        equalTo: "#password"
		        	           },
		        	registerType:"required",		        	
		  		    companyName:{
		  					     required:true,
		  					     maxlength:60
		  					   },
		        	email:     {
		        		  		required:true,
		        		  		email: true
		        			   },
		            tel:       "required",
		            sex:       "required",
		        	mobile:    {
		        		       required:true,
		        		       number:true
		        	           },
		        	accept:   "required"
				  },
				 messages: {
					 //userId:"请填写用户帐号",					           
			         realName: "请填写真实姓名",
			         password: {
			        	        required:"请填写密码",
			        	        minlength: $.validator.format("请至少输入{0}个字符."),
			        	        maxlength: $.validator.format("请最多输入{0}个字符."),
			         			},
			         password2:{
			        	 		required:"请再次输入密码",
			        	 		equalTo:"两次输入的密码不一致"
			         			},
			         registerType:"请选择注册企业类型",  
					 companyName:{
								 required:"请填写单位名称",
								 maxlength: $.validator.format("填写在工商局注册的名称，最多 {0} 字符"),
								 },
			         email:     {
			        	 		required:"请填写电子邮件",
			        	 		email:"电子邮件格式不正确"
			         			},
			         tel:       "请填写电话",
					 sex:       "请选择性别",
			         mobile:    {required:"请填写手机号码",
			        	         number:"手机号码格式不正确"
			        	        },			        
			         accept:    "请接受协议"       	
				  }
		    });
			$("#regFrom3").validate();
		},
		
		
		event : function (){			
			_this = this;
			
			
			$("#step1").on("click",function(){				
				if($("#regFrom1").validate().form()){
					registerEnCtrl.registerEn.step1();					
				}				
			});			
			
			$("#step2").on("click",function(){				
				if($("#regFrom2").validate().form()){
					registerEnCtrl.registerEn.step2();					
				}				
			});			
			
			$("#submitBtn").on("click",function(){	
				
				if(!$("#userId").val()){
					$("#userId-error").html('请填写用户帐号');
				}
				
				if($("#regFrom3").validate().form()){
					if(!$("#userId-error").text()){
						registerEnCtrl.registerEn.submit();	
					}				
				}				
			});
			
			//表单验证
			//_this.form1Validate();
			//_this.form2Validate();
			_this.form3Validate();
			
			$("#userId").on("blur",function(){
				_this.checkAccount();
			});
			
			
//			$.validator.addMethod("accountCheck", function(value, element) {				
//				 return this.optional(element) || (_this.checkAccount(value));
//			});
			
		},	
		
		 checkAccount : function(){
				var vo = {
				        userId : $("#userId").val()			
				}
				
				$.ajax({
		    		url :_this.urlItems.isExistURL,
		    		data : vo,
		    		dataType : "json",
		    		type : 'post',
		    		async:false,
		    		success : function(data) {				
		    			if(data.code !=_this.successCode){
		    				$("#userId-error").html(data.msg);
		    			}else{
		    				$("#userId-error").html("");
		    			}
		    		}
		    	});		
		},
		
		step1:function(){
			
			
			var registerTypeVal ;
			
			$(":radio").each(function(){
				if($(this).prop("checked")){
					registerTypeVal =  $(this).val()+"" ;
				}				   
			});
			
			var params = [];
			
			var paramType = {name:'',value:''};
			paramType.name= "registerType";
			paramType.value= registerTypeVal;
			params.push(paramType);
			
			$("#reg1Div input[type='text']").each(function() {
				if(!!$(this).attr("name")){
					var param = {name:'',value:''};
					param.name=$(this).attr("name");
					param.value= $(this).val();
					params.push(param);					
				}
			});	
			
			//基于ajaxfileUpload.js实现
			$.ajaxFileUpload({
				url : _this.urlItems.step1SaveURL,
				secureuri : false,
				data:params,
				fileElementId : 'licenseImg',//现使用方法
				dataType : 'json',
				success : function(data) {
					if (data.code == _this.successCode) {
						$('#reg1Div').addClass('fn-hide');
						$('#reg2Div').removeClass('fn-hide');
						$('#status #js_step1').removeClass('active');
						$('#status #js_step2').addClass('active');
						$("#hiddenId").val(data.result);
						//_this.id = data.result ;
					} else {
						window.webDialogUtil.alert("提示",data.msg);
					}
				},
				error : function(data, status, e){
					window.webDialogUtil.alert("提示",data.msg);
				}
			});
			
		},
		
		
		step2:function(){
			
			var busiDirtVal ;
			
			$(":radio").each(function(){
				if($(this).prop("checked")){
					busiDirtVal =  $(this).val()+"" ;
				}				   
			});
			
			var entity = {
					id:$("#hiddenId").val(),
					busiDirt : busiDirtVal,
					busiIndt : $("#busiIndt").val()	,
					salePrct : $("#salePrct").val(),				
					purchPrct : $("#purchPrct").val(),					
			}
			
			$.ajax({
	    		url :_this.urlItems.step2SaveURL,
	    		data : entity,
	    		dataType : "json",
	    		type : 'post',
	    		success : function(data) {				
	    			if(data.code==_this.successCode){ 
	    				$('#reg1Div').addClass('fn-hide');
	    				$('#reg2Div').addClass('fn-hide');
						$('#reg3Div').removeClass('fn-hide');
						$('#status #js_step1').removeClass('active');
						$('#status #js_step2').removeClass('active');
						$('#status #js_step3').addClass('active');
	    			}else{
	    				window.webDialogUtil.alert("提示",data.msg);
	    			}
	    		}
	    	});
			
		},
		
		submit : function (){
			
				var sexVal ;
			
				$(":radio").each(function(){
					if($(this).prop("checked")){
						sexVal =  $(this).val()+"" ;
					}				   
				});
				
				var registerTypeVal ;
				
				$(":radio").each(function(){
					if($(this).prop("checked")){
						registerTypeVal =  $(this).val()+"" ;
					}				   
				});
				
			
				var entity = {
					id:$("#hiddenId").val(),
					sex:sexVal,	
			        userId : $("#userId").val(),
					realName : $("#realName").val()	,
					password : $("#password").val(),
					registerType:registerTypeVal,
					companyName:$("#companyName").val(),
					email : $("#email").val(),
					tel: $("#tel").val(),
					mobile : $("#mobile").val(),
					activeState:"0",  //0-未激活
					auditState:"0"    //0-待审核
				}
		    	
		    	$.ajax({
		    		url :_this.urlItems.submitURL,
		    		data : entity,
		    		dataType : "json",
		    		type : 'post',
		    		success : function(data) {				
		    			if(data.code==_this.successCode){ 
		    				window.webDialogUtil.alert("提示",data.msg);	
		    				$("#regFrom3")[0].reset();
		    			}else{
		    				window.webDialogUtil.alert("提示",data.msg);
		    			}
		    		}
		    	});	
			
		},
},

$(function(){	
	registerEnCtrl.registerEn.event();
	
});